package com.feinno.module.security.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.feinno.framework.common.web.AbstractJQueryEntityController;
import com.feinno.framework.common.web.support.JsonResult;
import com.feinno.module.security.SecurityConstants;
import com.feinno.module.security.domain.Menu;
import com.feinno.module.security.service.MenuService;
import com.google.common.collect.Maps;

@Controller
@RequestMapping(value = "/manage/system/menu")
public class JsonMenuController extends
		AbstractJQueryEntityController<Menu, MenuService> {

	@Autowired
	private MenuService menuService;

	@Override
	protected Map<String, Boolean> getSortMap(HttpServletRequest request) {
		Map<String, Boolean> sortMap = Maps.newLinkedHashMap();
		sortMap.put("showState", true);
		sortMap.put("orderTime", false);
		return sortMap;
	}

	@RequestMapping(value = "moveTop")
	@ResponseBody
	public JsonResult moveTop(HttpServletRequest request,
			HttpServletResponse response) {
		JsonResult result = new JsonResult();
		try {
			String id = request.getParameter(getEntityIdName());
			menuService.moveTop(Long.valueOf(id));
			result.setMessage("置顶成功");
		} catch (Exception e) {
			handleException(result, "置顶", e);
		}
		return result;
	}

	@RequestMapping(value = "moveUp")
	@ResponseBody
	public JsonResult moveUp(HttpServletRequest request,
			HttpServletResponse response) {
		JsonResult result = new JsonResult();
		try {
			String id = request.getParameter(getEntityIdName());
			menuService.moveUp(Long.valueOf(id));
			result.setMessage("上移成功");
		} catch (Exception e) {
			handleException(result, "上移", e);
		}
		return result;
	}

	@Override
	protected void referenceData(HttpServletRequest request,
			Map<String, Object> model) {
		model.put("allIcons", SecurityConstants.FRAMEWORK_ALL_ICONS);
		model.put("allShowStates", SecurityConstants.SHOW_STATE_MAPPING);
	}

}
